package cn.ulc.socket.weapp.service.impl;

import cn.ulc.common.constant.PubDataConstant;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.util.CalendarUtil;
import cn.ulc.socket.weapp.common.BaseService;
import cn.ulc.socket.weapp.model.dto.CouponNewDTO;
import cn.ulc.socket.weapp.service.CouponServ;
import cn.ulc.store.dao.CouponDao;
import cn.ulc.store.dao.PubDataDao;
import cn.ulc.store.dao.user.UserCouponDao;
import cn.ulc.store.dao.user.WeixinUserDao;
import cn.ulc.store.entity.Company;
import cn.ulc.store.entity.Coupon;
import cn.ulc.store.entity.user.UserCoupon;
import cn.ulc.store.entity.user.WeixinUser;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class CouponServImpl extends BaseService implements CouponServ {

    CouponDao couponDao;
    PubDataDao pubDataDao;
    UserCouponDao userCouponDao;
    WeixinUserDao weixinUserDao;

    @Override
    @Transactional
    public CouponNewDTO findNewUserCoupon() {
        CouponNewDTO dto = new CouponNewDTO();
        Company company = getCurrentCompany();
        WeixinUser loginUser = getCurrentUser();
        String code = pubDataDao.findCodeByTypeAndName(PubDataConstant.DATA_TYPE_COUPON,"新手礼包");
        if(code!=null){
            Coupon coupon = couponDao.findByType(company.getId(),code);
            if(coupon!=null){
                UserCoupon userCoupon = new UserCoupon();
                setBaseInfo(userCoupon);
                userCoupon.setCouponId(coupon.getId());
                userCoupon.setIsFull(coupon.getIsFull());
                userCoupon.setFullAmount(coupon.getFullAmount());
                userCoupon.setUserScope(coupon.getUserScope());
                userCoupon.setGoodsId(coupon.getGoodsId());
                userCoupon.setGoodsTypeId(coupon.getGoodsTypeId());
                userCoupon.setAmount(coupon.getAmount());
                userCoupon.setIsUsed(false);
                String newCode = "C"+PubDataConstant.DATA_TYPE_COUPON+loginUser.getId();
                String maxCode = userCouponDao.findMaxCode(loginUser.getId(),newCode);
                Integer len = 13 - newCode.length();
                String num = "1";
                if(StringUtils.isNotBlank(maxCode)){
                    num =  (Integer.valueOf(maxCode.substring(newCode.length()))+1)+"";
                }
                len = len - num.length();
                for(int j = 0;j<len;j++){
                    newCode+="0";
                }
                userCoupon.setCode(newCode+num);
                userCoupon.setCompanyId(company.getId());
                userCoupon.setCouponId(coupon.getId());
                userCoupon.setStatus(PubDataConstant.COUPON_STATUS_USING);
                userCoupon.setTitle(coupon.getTitle());
                userCoupon.setWxUserId(loginUser.getId());
                userCoupon.setBeginTime(new Date());
                userCoupon.setIsForever(coupon.getIsForever());
                userCoupon.setRemark(coupon.getRemark());
                userCouponDao.save(userCoupon);
                loginUser.setLoginCount(loginUser.getLoginCount()+1);
                weixinUserDao.save(loginUser);
                dto.setUserCoupon(userCoupon);
                dto.setUser(loginUser);
            }
        }
        return dto;
    }

    @Override
    public void executeJob() {
//        Company company = getCurrentCompany();
        List<UserCoupon> couponList = userCouponDao.findListByUsed(false,false);
        if(couponList!=null && couponList.size()>0){
            Date date = CalendarUtil.beginDate(CalendarUtil.afterDays(new Date(),-1));
            List<UserCoupon> coupons = new ArrayList<>();
            for (UserCoupon coupon:couponList){
                Date endTime = CalendarUtil.beginDate(coupon.getEndTime());
                if(endTime.compareTo(date) < 0){   //已过期
                    coupon.setStatus(PubDataConstant.COUPON_STATUS_ENDED);
                    setBaseInfo(coupon);
                    coupons.add(coupon);
                }
            }
            userCouponDao.saveAll(coupons);
        }
    }
}
